package com.hluck.goodweatherapp.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hluck.goodweatherapp.data.bean.HourlyResponse;
import com.hluck.goodweatherapp.databinding.ItemHourlyRvBinding;
import com.hluck.goodweatherapp.utils.EasyDate;
import com.hluck.goodweatherapp.utils.WeatherUtil;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * @Author： LJH
 * @Time： 2024/1/6
 * @description：
 */
public class HourlyAdapter extends RecyclerView.Adapter<HourlyAdapter.ViewHolder> {

    private final List<HourlyResponse.HourlyBean> hourlyBeans;
    private OnClickItemCallback clickItemCallback;

    public void setClickItemCallback(OnClickItemCallback clickItemCallback) {
        this.clickItemCallback = clickItemCallback;
    }

    public HourlyAdapter(List<HourlyResponse.HourlyBean> hourlyBeans) {
        this.hourlyBeans = hourlyBeans;
    }

    @NonNull
    @NotNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        ItemHourlyRvBinding binding = ItemHourlyRvBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        ViewHolder viewHolder = new ViewHolder(binding);
        if (clickItemCallback != null) {
            binding.getRoot().setOnClickListener(v -> {
                clickItemCallback.onItemClick(viewHolder.getAdapterPosition());
            });
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull HourlyAdapter.ViewHolder holder, int position) {
        HourlyResponse.HourlyBean hourlyBean = hourlyBeans.get(position);
        String time = EasyDate.updateTime(hourlyBean.getFxTime());
        holder.binding.tvTime.setText(String.format("%s%s", EasyDate.showTimeInfo(time), time));
        WeatherUtil.changeIcon(holder.binding.ivStatus, Integer.parseInt(hourlyBean.getIcon()));
        holder.binding.tvTemperature.setText(String.format("%s℃", hourlyBean.getTemp()));
    }

    @Override
    public int getItemCount() {
        return hourlyBeans.size();
    }

    protected static class ViewHolder extends RecyclerView.ViewHolder{

        private ItemHourlyRvBinding binding;

        public ViewHolder(ItemHourlyRvBinding mbinding) {
            super(mbinding.getRoot());
            this.binding = mbinding;
        }
    }
}
